/*
 * Copyright (c) 2021 Huawei Device Co.,  Ltd.
 * Licensed under the Apache License,  Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,  software
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,  either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "iccreader_observer_proxy.h"
#include <string.h>
#include <ipc_types.h>
#include <message_option.h>
#include <message_parcel.h>

#include "iccreader_hilog.h"
#include "iccreader_errors.h"
#include "iccreader_common.h"

namespace SwanLink {
namespace Finance {

// 调用NAPI 的接口
void IccReaderObserverProxy::OnResult(int status, const ResultData &result) {
    sptr<IRemoteObject> remote = Remote();
    if (remote == nullptr) return;
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;
    
    ICCREADER_HILOGD(ICCREADER_SRV,  "OnResult");
    if (!data.WriteInterfaceToken(IccReaderObserverProxy::GetDescriptor())) {
        ICCREADER_HILOGE(ICCREADER_SRV,  "Write descriptor failed");
        return;
    }
    
    WRITE_PARCEL_NO_RET(data, Int32, status);
    WRITE_BUFFER_NO_RET(data, ResultData, &result, sizeof(result)); 
    
    int ret = remote->SendRequest(ICCREADER_OBSERVER_ID_ONSUCCESS,  data,  reply,  option);

    if (ret != ERR_OK) {
        ICCREADER_HILOGE(ICCREADER_SRV,  "SendRequest is failed,  ret: %d",  ret);
        return;
    }
}
void IccReaderObserverProxy::OnError(int status, const std::string &err) {
    sptr<IRemoteObject> remote = Remote();
    if (remote == nullptr) return;
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;
    
    ICCREADER_HILOGD(ICCREADER_SRV,  "OnError");
    if (!data.WriteInterfaceToken(IccReaderObserverProxy::GetDescriptor())) {
        ICCREADER_HILOGE(ICCREADER_SRV,  "Write descriptor failed");
        return;
    }
    
    WRITE_PARCEL_NO_RET(data, Int32, status);
    WRITE_PARCEL_NO_RET(data, String, err);
    
    int ret = remote->SendRequest(ICCREADER_OBSERVER_ID_ONERROR,  data,  reply,  option);

    if (ret != ERR_OK) {
        ICCREADER_HILOGE(ICCREADER_SRV,  "SendRequest is failed,  ret: %d",  ret);
        return;
    }
}
}  // namespace Finance
}  // namespace SwanLink
